package com.authoritydemo.controller;

import com.authoritydemo.entity.Users;
import com.authoritydemo.vo.UserProfileVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.servlet.http.HttpSession;
import org.springframework.ui.Model;

@Controller
public class PageController {

    @GetMapping("/index.html")
    public String index(HttpSession session, Model model) {
        Users user = (Users) session.getAttribute("user");
        if (user == null) {
            return "login";
        }
        model.addAttribute("user", user);
        return "index"; // 返回 templates/index.html
    }

    @GetMapping("/")
    public String root() {
        return "redirect:/index.html";
    }

    @GetMapping("/profile.html")
    public String showProfile(HttpSession session, Model model) {
        if (session.getAttribute("user") == null) {
            return "login";
        }

        UserProfileVO profile = (UserProfileVO) session.getAttribute("profile");
        if (profile == null) {
            return "redirect:/index.html";
        }

        model.addAttribute("profile", profile);
        return "profile"; // 对应 templates/profile.html
    }
}